import json

import requests
from openai import OpenAI

# 文档
# https://www.modelscope.cn/docs/model-service/API-Inference/intro
class ModelScopeAPI:
    def __init__(self, api_key: str):
        self.api_key = api_key

        self.base_url = "https://api-inference.modelscope.cn/v1/"
        self.client = OpenAI(
            api_key=self.api_key, # 请替换成您的ModelScope SDK Token
            base_url=self.base_url
        )

    def translate(self, message: str) -> str:
        headers = {"Authorization": f"Bearer {self.api_key}"}
        API_URL = "https://api-inference.modelscope.cn/api-inference/v1/models/iic/nlp_imt_translation_zh2en"
        payload = {
            "input": message
        }
        data = json.dumps(payload)
        response = requests.post(API_URL, headers=headers, data=data)
        print(response.status_code, response.json())

        return response.json()['Data']['translation']